<?php
require_once 'BaseController.php';

/**
 * 検索条件登録ボタン押下時処理
 *
 * @package apamanshop
 * @author M.Matsutani
 * @copyright コピーライト(要確認)
 */
class Mypage_KensakujoukenController extends Mypage_BaseController
{

	public function indexAction()
	{
		// パラメータの取得
		$this->_params = $this->getParameters();
		$result = "";
		//ログイン判定
		if ($this->isLogin())
		{
			$this->_params = $this->joukenConv($this->_params);

			//eIdの取得
			$cunnrentEId = $this->getCurrentUserEid();

			//登録モデルインスタンス生成
			$model = new Mypage_Model_Paraminsert();

			//ユーザＩＤ取得
			$userList = $model->getUId($cunnrentEId);

			$this->_params['UserCd'] = $userList[0]['M92_USER_CD'];

			//検索条件登録
			$resCd = $model->insertKensakuJouken($this->_params);

			//結果コード変換
			$result = Apm_Otheritems::getResMessage($resCd);

		}
		else
		{
			$result = "NOT_LOGIN";
		}

		$this->_mode = "json";
		$this->_isMobile = "1";

		// 返却処理
		$this->_api_data = array('result' => $result);

		$this->display();

	}

	public function updateAction()
	{
		// パラメータの取得
		$this->_params = $this->getParameters();
		$this->_mode = "json";
		$this->_isMobile = "1";

		$result = "";
		//ログイン判定
		if ($this->isLogin())
		{
			$this->_params = $this->joukenConv($this->_params);

			//eIdの取得
			$cunnrentEId = $this->getCurrentUserEid();

			//登録モデルインスタンス生成
			$model = new Mypage_Model_Paraminsert();
			//ユーザＩＤ取得
			$userList = $model->getUId($cunnrentEId);

			$this->_params['UserCd'] = $userList[0]['M92_USER_CD'];

			//検索条件登録
			$res = $model->updateKensakuJouken($this->_params);

			$this->_api_data = $res;
		}
		else
		{
			$result = "NOT_LOGIN";

			// 返却処理
			$this->_api_data = array('result' => $result);
		}


		$this->display();

	}

	/**
	 * 条件変換
	 *
	 * @param array $params
	 *
	 */
	private function joukenConv($params)
	{

		//AreaCd変換
		$cntArea = count($params['AreaCd']);
		if($cntArea > 0)
		{
			if (is_array($params['AreaCd']))
			{
				$strArea = "";
				for($i=0;$i<$cntArea;$i++)
				{
					if($i == 0)
					{
						$strArea .= $params['AreaCd'][$i];
					}else{
						$strArea .= "," . $params['AreaCd'][$i];
					}
				}
				$params['AreaCd'] = "";
				$params['AreaCd'] = $strArea;
			}
		}
		//prefCd変換
		$cntPref = count($params['PrefCd']);
		if($cntPref > 0)
		{
			$strPref = "";
			for($i=0;$i<$cntPref;$i++)
			{
				if($i == 0)
				{
					$strPref .= $params['PrefCd'][$i];
				}else{
					$strPref .= "," . $params['PrefCd'][$i];
				}
			}
			$params['PrefCd'] = "";
			$params['PrefCd'] = $strPref;
		}
		//CityCd変換
		$cntCity = count($params['CityCd']);
		if($cntCity > 0)
		{
			$strCity = "";
			for($i=0;$i<$cntCity;$i++)
			{
				if ($i == 0)
				{
					$strCity .= $params['CityCd'][$i];
				}else{
					$strCity .= "," . $params['CityCd'][$i];
				}
			}
			$params['CityCd'] = "";
			$params['CityCd'] = $strCity;
		}
		//SectCd変換
		$cntSect = count($params['TownCd']);
		if($cntSect > 0)
		{
			$strSect = "";
			for($i=0;$i<$cntSect;$i++)
			{
				if ($i == 0)
				{
					$strSect .= $params['TownCd'][$i];
				}else{
					$strSect .= "," . $params['TownCd'][$i];
				}
			}
			$params['SectCd'] = "";
			$params['SectCd'] = $strSect;
		}
		//RailCd変換
		$cntRail = count($params['RailCd']);
		if($cntRail > 0)
		{
			$strRail = "";
			for($i=0;$i<$cntRail;$i++)
			{
				if ($i == 0)
				{
					$strRail .= $params['RailCd'][$i];
				}else{
					$strRail .= "," . $params['RailCd'][$i];
				}
			}
			$params['RailCd'] = "";
			$params['RailCd'] = $strRail;
		}
		//StCd変換
		$cntSt = count($params['StCd']);
		if($cntSt > 0)
		{
			$strSt = "";
			for($i=0;$i<$cntSt;$i++)
			{
				if ($i == 0)
				{
					$strSt .= $params['StCd'][$i];
				}else{
					$strSt .= "," . $params['StCd'][$i];
				}
			}
			$params['StCd'] = "";
			$params['StCd'] = $strSt;
		}

		return $params;
	}
}